import java.util.PriorityQueue;

 public  class KthLargest {
    private  PriorityQueue<Integer> q;
    private  int[] arr;
    private  int k;
    public KthLargest(int k, int[] nums) {

        q=new PriorityQueue<Integer>();
        this.k=k;
        this.arr=nums;
        for(int i=0;i<k;i++){
            q.add(arr[i]);
        }
        for(int i=k;i<arr.length;i++){
            if(q.peek()<arr[i]){
                q.poll();
                q.add(arr[i]);
            }
        }

    }
    
    public int add(int val) {

      if(val>q.peek()){
        q.poll();
        q.add(val);
      }
      return q.peek();
    }
}